package com.alpesressources.comexpert.bo.request;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.apache.log4j.Logger;

import com.alpesressources.comexpert.bo.IObject;
import com.alpesressources.comexpert.util.Md5Hash;

public class Request {
	private String token;
	private Date requestDate;
	private String hashKey;
	private IObject data;

	public static final String SALT = "lottux";
	private static Logger logger = Logger.getLogger(Request.class);

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public Date getRequestDate() {
		return requestDate;
	}

	public void setRequestDate(Date requestDate) {
		this.requestDate = requestDate;
	}

	public String getHashKey() {
		return hashKey;
	}

	public void setHashKey(String hashKey) {
		this.hashKey = hashKey;
	}

	public IObject getData() {
		return data;
	}

	public void setData(IObject data) {
		this.data = data;
	}

	public boolean isHashKeyValid() {
		String validHashKey = "";
		String validkey="";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
		sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

		if (data != null) {
			validkey =SALT + "_" + token + "_"
					+ sdf.format(requestDate) + "_" + data.getConcatenation();
		} else {
			validkey = SALT+"_"+token+"_"+sdf.format(requestDate);
		}

		validHashKey=Md5Hash.getHash(validkey);
		
		logger.debug("validHashKey=" + validHashKey);

		return validHashKey.equals(hashKey);
	}

	@Override
	public String toString() {
		return "Request [token=" + token + ", requestDate=" + requestDate
				+ ", hashKey=" + hashKey + ", data=" + data + "]";
	}

}
